<template>
  <div class="login-group">

    <div class="login-info">
      <h2>后台管理登录</h2>
      <el-form :model="loginInfo" :rules="rules" ref="loginForm" label-position="right" label-width="80px" @keydown.enter.native="login('loginForm')">

        <el-form-item prop="username">
          <span class="span-info" slot="label">用户名</span>
          <el-input v-model="loginInfo.username" prefix-icon="el-icon-user" placeholder="请输入用户名"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <span class="span-info" slot="label">密&nbsp;&nbsp;&nbsp;&nbsp;码</span>
          <el-input v-model="loginInfo.password" prefix-icon="el-icon-lock" show-password
                    placeholder="请输入密码"></el-input>
        </el-form-item>
        <el-form-item>
          <span class="span-info" slot="label">选择类型</span>
          <el-radio-group v-model="loginInfo.userType">
            <el-radio :label="1">用户</el-radio>
            <el-radio :label="2">管理员</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item>
          <el-button @click="login('loginForm')" type="primary">登录</el-button>
          <el-button type="success">注册</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Login',
  data() {
    return {
      loginInfo: {
        username: '',
        password: '',
        userType: 1
      },
      rules: {
        username: [
          {
            required: true,
            message: '用户名不为空',
            trigger: 'blur'
          },
          {
            min: 2,
            max: 5,
            message: '长度在2到5之间',
            trigger: 'blur'
          }
        ],
        password: [
          {
            required: true,
            message: '密码不为空',
            trigger: 'blur'
          },
          {
            min: 5,
            max: 15,
            message: '长度在 5 到 15 个字符',
            trigger: 'blur'
          }
        ]
      },
      loading: false
    }
  },
  methods: {
    login(ref) {
      this.$refs[ref].validate(vaild => {
          if (vaild) {
            /* 开启遮罩层 */
            this.loading = this.$loading({text: '加载中……'})
            this.$http.post('account/login', this.loginInfo).then(({data}) => {

              if (data.code === 20000) {
                // 获取token存入cookie
                this.$cookies.set('token', data.data.token)
                /* 提示信息，页面跳转 */
                this.$message({
                  type: 'success',
                  message: '登录成功',
                  onClose: () => {
                    this.$router.replace('/')
                  }
                })
              } else {
                this.$alert(data.msg, {type: 'error'})
              }
            }).catch(err => {
              console.error(err)
              this.$alert('与服务器连接失败，请稍后再试……', {type: 'error'})
            }).finally(() => {
              this.loading.close()
            })
          }
        }
      )
    }
  },
  created() {
    this.$cookies.remove('token')
  }
}
</script>

<style scoped lang="scss">
.login-group {
  height: 100vh;
  width: 100vw;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  background: url("../assets/images/login-bg.jpg") no-repeat center/cover;

  h2 {
    margin-bottom: 20px;
    color: orange;
  }

  .login-info {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    height: 40vh;
    width: 33vw;
    border-radius: 10px;
    background-color: rgba(255, 255, 255, .3);
    padding: 40px 20px;

    .span-info {
      color: orange;
    }
  }
}
</style>
